//
//  PersonCenterViewController.m
//  Inforcollection
//
//  Created by macbook on 15/12/10.
//  Copyright © 2015年 oceansoft.liw. All rights reserved.
//

#import "PersonCenterViewController.h"
#import "PersonInfoView.h"
#import "PersonChangePassWordView.h"
#import "PersonChangePhoneView.h"

#define MScrollTag 1000


@interface PersonCenterViewController ()<jumpToLoginDelegate>
{
    HMSegmented *hmSegments;
    PersonInfoView *infoView;
}
@end

@implementation PersonCenterViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initViews];    
}

- (void)initViews
{
    [self setNaviBarTitle:@"个人信息"];
    [self addScrollView];
    [self addContentSegment];
    [self addTableViewContent];
}

- (void)addScrollView
{
    _personScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, NaviBarHeight+StatuBarHeight+KSegmentHeight, ScreenWidth, ScreenHeight-NaviBarHeight-StatuBarHeight-KSegmentHeight)];
    _personScrollView.showsHorizontalScrollIndicator = NO;
    _personScrollView.showsVerticalScrollIndicator = NO;
    _personScrollView.tag = MScrollTag;
    _personScrollView.contentSize = CGSizeMake(ScreenWidth*3, _personScrollView.bounds.size.height);
    _personScrollView.delegate = self;
    _personScrollView.pagingEnabled = YES;
    [self.view addSubview:_personScrollView];
}

- (void)addContentSegment
{
    hmSegments = [[HMSegmented alloc] initWithFrame:CGRectMake(0, NaviBarHeight+StatuBarHeight, ScreenWidth, KSegmentHeight)];
    hmSegments.sectionTitles = [NSArray arrayWithObjects:@"个人信息",@"修改密码",@"更换手机号", nil];
    [hmSegments awakeFromNib];
    __weak typeof (self)weakSelf = self;
    [hmSegments setIndexChangeBlock:^(NSInteger index) {
        [weakSelf.personScrollView scrollRectToVisible:CGRectMake(ScreenWidth*index,0,ScreenWidth,weakSelf.personScrollView.bounds.size.height) animated:YES];
    }];
    [hmSegments addTarget:self action:@selector(hmSegmentValue:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:hmSegments];
}

- (void)addTableViewContent
{
    infoView = [[PersonInfoView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-StatuBarHeight-NaviBarHeight-KSegmentHeight)];
    [_personScrollView addSubview:infoView];
    
    PersonChangePassWordView *passwordView = [[PersonChangePassWordView alloc] initWithFrame:CGRectMake(ScreenWidth, 0, ScreenWidth, ScreenHeight-StatuBarHeight-NaviBarHeight-KSegmentHeight)];
    
    passwordView.jumpToLoginDelegate =self;
    
    [_personScrollView addSubview:passwordView];
    
    PersonChangePhoneView *phoneView = [[PersonChangePhoneView alloc] initWithFrame:CGRectMake(ScreenWidth*2, 0, ScreenWidth, ScreenHeight-StatuBarHeight-NaviBarHeight-KSegmentHeight)];
    [_personScrollView addSubview:phoneView];

}

- (void)jumpToLogin
{
    [[UserData shareUserData] loginOut];
    
    [self toLoginVC];
    
    self.tabBarController.selectedIndex=0;
    
    [self.navigationController popToRootViewControllerAnimated:YES];
    
    [SharedNotifiCenter postNotificationName:isLoginSuccess object:nil];
}

#pragma mark -Scrollview Delegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (scrollView.tag == MScrollTag) {
        CGFloat pageWith = scrollView.bounds.size.width;
        int page = scrollView.contentOffset.x/pageWith;
        [hmSegments setSelectedSegmentIndex:page animated:YES];
        [self hmSegmentValue:hmSegments];
    }
}
#pragma mark -分段选择进入界面时 界面会调用的
- (void)hmSegmentValue:(HMSegmented *)segment
{

    switch (segment.selectedSegmentIndex) {
        case 0:

            [infoView changePhoneNumber];
            break;
            
        case 1:

            [infoView changePhoneNumber];
            break;
        case 2:
            
            break;
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
